package controllers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import play.libs.OAuth2;
import javax.persistence.Query;

import models.Comment;
import models.Event;
import models.Event.Category;
import models.User.ProfileTypes;
import models.User;
import play.data.validation.Required;
import play.libs.Crypto;
import play.mvc.Controller;
import play.mvc.Http;
import play.mvc.With;
//@Check("NormalUser")
//@With(MySecure.class)
public class Application extends Controller {
	
		
	public static void home() {
		User user = User.find("byEmail", session.get("username")).first();
		if(user==null) {
			UserManager.index();
		}
		else {			
			List<Event> events = Event.find("select e from Event e where e.eventDate >= CURRENT_DATE order by e.eventDate").fetch();
			
			Calendar cal = Calendar.getInstance();
			Date today = new Date();
			cal.setTime(today);
			int year = cal.get(Calendar.YEAR);
			int day = cal.get(Calendar.DAY_OF_YEAR);

			
			List<Event> actualEvents = new ArrayList<Event>();
			List<Event> importantEvent = new ArrayList<Event>();
			importantEvent.addAll(user.registeredEvents);
			importantEvent.addAll(user.ownEvents);
			for (Event event: importantEvent)
			{
				if (event.selected == false)
				{
				   cal.setTime(event.eventDate);
				   
				   if (cal.get(Calendar.YEAR) == year &&
					    (cal.get(Calendar.DAY_OF_YEAR) - day) == 0)
				   {
			          event.date = "today";
				      actualEvents.add(event);					
				   }
				
				   if (cal.get(Calendar.YEAR) == year &&
				         (cal.get(Calendar.DAY_OF_YEAR) - day) == 1)
				   {
				      event.date = "tomorrow";
				      actualEvents.add(event);					
				   }
				}
			}			
			
			List<Event> invitations = user.invitations;
			
			render(events, actualEvents, user, invitations);
		}
	}
	
}